﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetAllChildClass_V2]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetAllChildClass_V2];
GO
CREATE PROCEDURE [dbo].[sproc_GetAllChildClass_V2]
    @Class_id int
/*

========================================================
功能:    得到该类的所有子类
参数:
    @Class_id int        :    组（类）ID
备注:可以优化
========================================================

*/
AS
BEGIN
SET NOCOUNT ON;

WITH Class_Subtree ( ClassID )
AS
(
    SELECT ClassID
        FROM dbo.UDS_Class
        WHERE ClassParentID = @class_id AND ClassParentID <> ClassID

    UNION ALL

    SELECT c.ClassID
        FROM dbo.UDS_Class c INNER JOIN Class_Subtree s ON c.ClassParentID = s.ClassID AND c.ClassParentID <> c.ClassID
)
SELECT * 
FROM dbo.UDS_Class
WHERE ClassID IN ( SELECT ClassID FROM Class_Subtree );

END
GO